package main

import (
	"flag"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	"github.com/ifnk/micro-blog/internal/permission"
	"github.com/ifnk/micro-blog/internal/pkg/config"
	"github.com/ifnk/micro-blog/internal/pkg/dbcontext"
	"github.com/ifnk/micro-blog/internal/pkg/log"
	"github.com/ifnk/micro-blog/protobuf/permission_pb"
	"github.com/lib/pq"
	"github.com/pkg/errors"
	uuid "github.com/satori/go.uuid"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	"net"
)

// 命令行可以 传 config.yaml 的路径 , 默认 的值的是  ./configs/config.yaml
var flagConfig = flag.String("config", "./configs/config.yaml", "path to config file")

func main() {
	flag.Parse()

	logger := log.New()
	// 加载 配置文件 config.yaml 获取 config 实体
	conf, err := config.Load(*flagConfig)
	if err != nil {
		logger.Fatal(err)
	}
	logger.Infof("加载的配置文件为 -> %+v", conf)

	gormDB, jinZhuDB := InitDB(logger, conf)

	//_, jinzhuDB, err := dbcontext.NewPermissionDB(conf)

	if err != nil {
		logger.Fatal(err)
	}

	grpcServer := grpc.NewServer()

	// user repo 的接口 注册 为grpc服务
	permission_pb.RegisterUserServiceServer(grpcServer, InitUserServer(logger, gormDB, jinZhuDB))
	permission_pb.RegisterCasbinServiceServer(grpcServer, InitCasbinServer(logger, gormDB, jinZhuDB))
	permission_pb.RegisterAuthorityServiceServer(grpcServer, InitAuthorityServer(logger, gormDB, jinZhuDB))
	permission_pb.RegisterMenuServiceServer(grpcServer, InitMenuServer(logger, gormDB, jinZhuDB))
	permission_pb.RegisterApiServiceServer(grpcServer, InitApiServer(logger, gormDB, jinZhuDB))
	permission_pb.RegisterUserRltAuthorityServiceServer(grpcServer, InitUserRltAuthorityServer(logger, gormDB, jinZhuDB))
	// 这个角色 服务 只 实现了 基本 的crud ,所以就用他默认 的 AuthorityServiceDefaultServer 注册 ，如果要
	// 写自定义的 业务的话，就 在他外面包一层， 参看 上面 两行的 user 和  UserRltAuth  server 的写法
	//permission_pb.RegisterAuthorityServiceServer(grpcServer, &permission_pb.AuthorityServiceDefaultServer{DB: jinzhuDB.DB})
	// todo 这里 除了注册 userService 意外 ，还注册  RegisterUserServiceServer 和 UserRltAuthServiceServer
	// 注册 grpc 反射服务   Server 端添加后可以通过该服务获取所有服务的信息，包括服务定义，方法，属性等
	reflection.Register(grpcServer)

	// 监听 user 服务 grpc 端口 50051
	lis, err := net.Listen("tcp", conf.Permission.Server.GRPC.Port)
	logger.Infof("监听 user 服务 grpc 端口 嘟嘟    %s", conf.Permission.Server.GRPC.Port)
	if err != nil {
		logger.Errorf(" 监听 user 服务 grpc 端口 50051 失败  failed to listen: %v", err)
	}

	if err = grpcServer.Serve(lis); err != nil {
		logger.Errorf("user 服务 grpc 启动失败!  failed to serve: %v", err)
	}

}

func InitDB(logger *log.Logger, conf *config.Config) (*dbcontext.GormDB, *dbcontext.JinZhuDB) {
	gormDB, jinzhuDB, err := dbcontext.NewPermissionDB(conf)
	if err != nil {
		logger.Fatal("数据库  permission   连接失败! err -> %+v", err)
	}

	// 自动 迁移 表
	err = gormDB.AutoMigrate(
		&permission_pb.UserORM{},
		&permission_pb.AuthorityORM{},
		&permission_pb.UserRltAuthorityORM{},
		&permission_pb.MenuORM{},
		&permission_pb.ApiORM{},
		&gormadapter.CasbinRule{},
	)
	if err != nil {
		logger.Fatal("数据表  全部  迁移失败! err -> %+v", err)
	}

	// 初始化 用户 种子 数据
	InitUserData(gormDB, logger)
	// 初始化 角色  种子 数据
	InitAuthorityData(gormDB, logger)
	// 初始化 菜单  种子 数据
	InitMenuData(gormDB, logger)
	// 初始化 api  种子 数据
	InitApiData(gormDB, logger)
	// 初始化 casbin  种子 数据
	InitCasbinData(gormDB, logger)

	// 初始化 用户角色 关联   种子 数据
	InitUserRltAuthorityData(gormDB, logger)

	// 初始化 角色 菜单 关联   种子 数据
	return gormDB, jinzhuDB
}

func InitUserData(db *dbcontext.GormDB, logger *log.Logger) {
	var count int64
	db.Model(&permission_pb.UserORM{}).Count(&count)
	if count == 0 {
		//  表为空的话 创建种子数据
		logger.Infof("users表数据为空,将创建种子数据")
		entities := []permission_pb.UserORM{
			{UUID: uuid.NewV4(), Username: "admin", Password: "e10adc3949ba59abbe56e057f20f883e", NickName: "超级管理员", HeaderImg: "https://qmplusimg.henrongyi.top/gva_header.jpg", Phone: "17611111111", Email: "333333333@qq.com", AuthorityIds: pq.Int64Array{888, 8881, 9528}},
			{UUID: uuid.NewV4(), Username: "a303176530", Password: "3ec063004a6f31642261936a379fde3d", NickName: "QMPlusUser", HeaderImg: "https:///qmplusimg.henrongyi.top/1572075907logo.png", Phone: "17611111111", Email: "333333333@qq.com", AuthorityIds: pq.Int64Array{888}},
		}
		err := db.Create(&entities).Error
		if err != nil {
			err = errors.Wrap(err, "users  表数据初始化失败!")
			logger.Fatal(err)
		}
	}
}

func InitUserRltAuthorityServer(logger *log.Logger, gormDB *dbcontext.GormDB, jinzhuDB *dbcontext.JinZhuDB) *permission.UserRltAuthServer {
	server := permission.NewUserRltAuthServer(logger, gormDB, jinzhuDB)
	return &server
}
func InitAuthorityServer(logger *log.Logger, gormDB *dbcontext.GormDB, jinzhuDB *dbcontext.JinZhuDB) *permission.AuthorityServer {
	server := permission.NewAuthorityServer(logger, gormDB, jinzhuDB)
	return &server
}
func InitCasbinServer(logger *log.Logger, gormDB *dbcontext.GormDB, jinzhuDB *dbcontext.JinZhuDB) *permission.CasbinServer {
	server := permission.NewCasbinServer(logger, gormDB, jinzhuDB)
	return &server
}
func InitMenuServer(logger *log.Logger, gormDB *dbcontext.GormDB, jinzhuDB *dbcontext.JinZhuDB) *permission.MenuServer {
	server := permission.NewMenuServer(logger, gormDB, jinzhuDB)
	return &server
}
func InitApiServer(logger *log.Logger, gormDB *dbcontext.GormDB, jinzhuDB *dbcontext.JinZhuDB) *permission.ApiServer {
	server := permission.NewApiServer(logger, gormDB, jinzhuDB)
	return &server
}

func InitUserServer(logger *log.Logger, gormDB *dbcontext.GormDB, jinzhuDB *dbcontext.JinZhuDB) *permission.Server {
	repository := permission.NewRepository(logger, gormDB)
	server := permission.NewUserServer(logger, repository, gormDB, jinzhuDB)
	return &server
}

func InitUserRltAuthorityData(db *dbcontext.GormDB, logger *log.Logger) {
	var count int64
	db.Model(&permission_pb.UserRltAuthorityORM{}).Count(&count)
	if count == 0 {
		//  表为空的话 创建种子数据
		logger.Infof("user authority 关联 表数据为空,将创建种子数据")
		entities := []permission_pb.UserRltAuthorityORM{
			{UserId: 1, AuthorityId: 888},
			{UserId: 1, AuthorityId: 8881},
			{UserId: 1, AuthorityId: 9528},
			{UserId: 2, AuthorityId: 888},
		}
		err := db.Create(&entities).Error
		if err != nil {
			err = errors.Wrap(err, "user authority 关联  表数据初始化失败!")
			logger.Fatal(err)
		}
	}
}

func InitCasbinData(db *dbcontext.GormDB, logger *log.Logger) {
	var count int64
	db.Model(&gormadapter.CasbinRule{}).Count(&count)
	if count == 0 {
		//  表为空的话 创建种子数据
		logger.Infof("menu表数据为空,将创建种子数据")
		entities := []gormadapter.CasbinRule{
			{Ptype: "p", V0: "888", V1: "/base/login", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/user/register", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/api/createApi", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/api/getApiList", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/api/getApiById", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/api/deleteApi", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/api/updateApi", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/api/getAllApis", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/api/deleteApisByIds", V2: "DELETE"},

			{Ptype: "p", V0: "888", V1: "/authority/copyAuthority", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/authority/updateAuthority", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/authority/createAuthority", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/authority/deleteAuthority", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/authority/getAuthorityList", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/authority/setDataAuthority", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/menu/getMenu", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/menu/getMenuList", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/menu/addBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/menu/getBaseMenuTree", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/menu/addMenuAuthority", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/menu/getMenuAuthority", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/menu/deleteBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/menu/updateBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/menu/getBaseMenuById", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/api/post", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/api/post/:id", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/api/post", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/api/post/:id", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/api/post/:id", V2: "DELETE"},

			{Ptype: "p", V0: "888", V1: "/api/comment", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/api/comment/:id", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/api/comment", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/api/comment/:id", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/api/comment/:id", V2: "DELETE"},

			{Ptype: "p", V0: "888", V1: "/user/getUserInfo", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/api/user", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/api/user/:id", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/api/dict", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/api/dict/:id", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/user/setUserInfo", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/user/setSelfInfo", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/user/getUserList", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/user/deleteUser", V2: "DELETE"},
			{Ptype: "p", V0: "888", V1: "/user/changePassword", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/user/setUserAuthority", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/user/setUserAuthorities", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/user/resetPassword", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/findFile", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/breakpointContinueFinish", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/breakpointContinue", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/removeChunk", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/upload", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/casbin/updateCasbin", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/jwt/jsonInBlacklist", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/system/getSystemConfig", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/system/setSystemConfig", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/system/getServerInfo", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/customer/customer", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/customer/customer", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/customer/customer", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/customer/customer", V2: "DELETE"},
			{Ptype: "p", V0: "888", V1: "/customer/customerList", V2: "GET"},

			{Ptype: "p", V0: "888", V1: "/autoCode/getDB", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/autoCode/getMeta", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/autoCode/preview", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/autoCode/getTables", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/autoCode/getColumn", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/autoCode/rollback", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/autoCode/createTemp", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/autoCode/delSysHistory", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/autoCode/getSysHistory", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/findSysDictionaryDetail", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/updateSysDictionaryDetail", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/createSysDictionaryDetail", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/getSysDictionaryDetailList", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/deleteSysDictionaryDetail", V2: "DELETE"},

			{Ptype: "p", V0: "888", V1: "/sysDictionary/findSysDictionary", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/sysDictionary/updateSysDictionary", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/sysDictionary/getSysDictionaryList", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/sysDictionary/createSysDictionary", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/sysDictionary/deleteSysDictionary", V2: "DELETE"},

			{Ptype: "p", V0: "888", V1: "/sysOperationRecord/findSysOperationRecord", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/sysOperationRecord/updateSysOperationRecord", V2: "PUT"},
			{Ptype: "p", V0: "888", V1: "/sysOperationRecord/createSysOperationRecord", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/sysOperationRecord/getSysOperationRecordList", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecord", V2: "DELETE"},
			{Ptype: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecordByIds", V2: "DELETE"},

			{Ptype: "p", V0: "888", V1: "/email/emailTest", V2: "POST"},

			{Ptype: "p", V0: "888", V1: "/simpleUploader/upload", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/simpleUploader/checkFileMd5", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/simpleUploader/mergeFileMd5", V2: "GET"},

			{Ptype: "p", V0: "888", V1: "/excel/importExcel", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/excel/loadExcel", V2: "GET"},
			{Ptype: "p", V0: "888", V1: "/excel/exportExcel", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/excel/downloadTemplate", V2: "GET"},

			{Ptype: "p", V0: "888", V1: "/authorityBtn/setAuthorityBtn", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/authorityBtn/getAuthorityBtn", V2: "POST"},
			{Ptype: "p", V0: "888", V1: "/authorityBtn/canRemoveAuthorityBtn", V2: "POST"},

			{Ptype: "p", V0: "8881", V1: "/base/login", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/user/register", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/api/createApi", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/api/getApiList", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/api/getApiById", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/api/deleteApi", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/api/updateApi", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/api/getAllApis", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/authority/createAuthority", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/authority/deleteAuthority", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/authority/getAuthorityList", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/authority/setDataAuthority", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/getMenu", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/getMenuList", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/addBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/getBaseMenuTree", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/addMenuAuthority", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/getMenuAuthority", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/deleteBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/updateBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/menu/getBaseMenuById", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/user/changePassword", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/user/getUserList", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/user/setUserAuthority", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/fileUploadAndDownload/upload", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/casbin/updateCasbin", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/jwt/jsonInBlacklist", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/system/getSystemConfig", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/system/setSystemConfig", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "POST"},
			{Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "PUT"},
			{Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "DELETE"},
			{Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "GET"},
			{Ptype: "p", V0: "8881", V1: "/customer/customerList", V2: "GET"},
			{Ptype: "p", V0: "8881", V1: "/user/getUserInfo", V2: "GET"},

			{Ptype: "p", V0: "9528", V1: "/base/login", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/user/register", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/api/createApi", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/api/getApiList", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/api/getApiById", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/api/deleteApi", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/api/updateApi", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/api/getAllApis", V2: "POST"},

			{Ptype: "p", V0: "9528", V1: "/authority/createAuthority", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/authority/deleteAuthority", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/authority/getAuthorityList", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/authority/setDataAuthority", V2: "POST"},

			{Ptype: "p", V0: "9528", V1: "/menu/getMenu", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/menu/getMenuList", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/menu/addBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/menu/getBaseMenuTree", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/menu/addMenuAuthority", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/menu/getMenuAuthority", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/menu/deleteBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/menu/updateBaseMenu", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/menu/getBaseMenuById", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/user/changePassword", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/user/getUserList", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/user/setUserAuthority", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/upload", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/casbin/updateCasbin", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/jwt/jsonInBlacklist", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/system/getSystemConfig", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/system/setSystemConfig", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "PUT"},
			{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "GET"},
			{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "DELETE"},
			{Ptype: "p", V0: "9528", V1: "/customer/customerList", V2: "GET"},
			{Ptype: "p", V0: "9528", V1: "/autoCode/createTemp", V2: "POST"},
			{Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"},
		}
		err := db.Create(&entities).Error
		if err != nil {
			err = errors.Wrap(err, "casbin  表数据初始化失败!")
			logger.Fatal(err)
		}
	}
}

func InitMenuData(db *dbcontext.GormDB, logger *log.Logger) {
	var count int64
	db.Model(&permission_pb.MenuORM{}).Count(&count)
	if count == 0 {
		//  表为空的话 创建种子数据
		logger.Infof("menu表数据为空,将创建种子数据")
		entities := []permission_pb.MenuORM{
			{Id: 1, MenuLevel: 0, Hidden: false, ParentId: 0, Path: "dashboard", Name: "dashboard", Component: "view/dashboard/index.vue", Sort: 1, Title: "仪表盘", Icon: "odometer"},
			{Id: 2, MenuLevel: 0, Hidden: false, ParentId: 0, Path: "about", Name: "about", Component: "view/about/index.vue", Sort: 7, Title: "关于我们", Icon: "info-filled"},
			{Id: 3, MenuLevel: 0, Hidden: false, ParentId: 0, Path: "admin", Name: "superAdmin", Component: "view/superAdmin/index.vue", Sort: 3, Title: "超级管理员", Icon: "user"},
			{Id: 4, MenuLevel: 0, Hidden: false, ParentId: 3, Path: "authority", Name: "authority", Component: "view/superAdmin/authority/authority.vue", Sort: 1, Title: "角色管理", Icon: "avatar"},
			{Id: 5, MenuLevel: 0, Hidden: false, ParentId: 3, Path: "menu", Name: "menu", Component: "view/superAdmin/menu/menu.vue", Sort: 2, Title: "菜单管理", Icon: "tickets", KeepAlive: true},
			{Id: 6, MenuLevel: 0, Hidden: false, ParentId: 3, Path: "api", Name: "api", Component: "view/superAdmin/api/api.vue", Sort: 3, Title: "api管理", Icon: "platform", KeepAlive: true},
			{Id: 7, MenuLevel: 0, Hidden: false, ParentId: 3, Path: "user", Name: "user", Component: "view/superAdmin/user/user.vue", Sort: 4, Title: "用户管理", Icon: "coordinate"},
			{Id: 8, MenuLevel: 0, Hidden: true, ParentId: 0, Path: "person", Name: "person", Component: "view/person/person.vue", Sort: 4, Title: "个人信息", Icon: "message"},
			{Id: 9, MenuLevel: 0, Hidden: false, ParentId: 0, Path: "example", Name: "example", Component: "view/example/index.vue", Sort: 6, Title: "示例文件", Icon: "management"},
			{Id: 10, MenuLevel: 0, Hidden: false, ParentId: 9, Path: "excel", Name: "excel", Component: "view/example/excel/excel.vue", Sort: 4, Title: "excel导入导出", Icon: "takeaway-box"},
			{Id: 11, MenuLevel: 0, Hidden: false, ParentId: 9, Path: "upload", Name: "upload", Component: "view/example/upload/upload.vue", Sort: 5, Title: "媒体库（上传下载）", Icon: "upload"},
			{Id: 12, MenuLevel: 0, Hidden: false, ParentId: 9, Path: "breakpoint", Name: "breakpoint", Component: "view/example/breakpoint/breakpoint.vue", Sort: 6, Title: "断点续传", Icon: "upload-filled"},
			{Id: 13, MenuLevel: 0, Hidden: false, ParentId: 9, Path: "customer", Name: "customer", Component: "view/example/customer/customer.vue", Sort: 7, Title: "客户列表（资源示例）", Icon: "avatar"},
			{Id: 14, MenuLevel: 0, Hidden: false, ParentId: 0, Path: "systemTools", Name: "systemTools", Component: "view/systemTools/index.vue", Sort: 5, Title: "系统工具", Icon: "tools"},
			{Id: 15, MenuLevel: 0, Hidden: false, ParentId: 14, Path: "autoCode", Name: "autoCode", Component: "view/systemTools/autoCode/index.vue", Sort: 1, Title: "代码生成器", Icon: "cpu", KeepAlive: true},
			{Id: 16, MenuLevel: 0, Hidden: false, ParentId: 14, Path: "formCreate", Name: "formCreate", Component: "view/systemTools/formCreate/index.vue", Sort: 2, Title: "表单生成器", Icon: "magic-stick", KeepAlive: true},
			{Id: 17, MenuLevel: 0, Hidden: false, ParentId: 14, Path: "system", Name: "system", Component: "view/systemTools/system/system.vue", Sort: 3, Title: "系统配置", Icon: "operation"},
			{Id: 18, MenuLevel: 0, Hidden: false, ParentId: 3, Path: "dictionary", Name: "dictionary", Component: "view/superAdmin/dictionary/sysDictionary.vue", Sort: 5, Title: "字典管理", Icon: "notebook"},
			{Id: 19, MenuLevel: 0, Hidden: true, ParentId: 3, Path: "dictionaryDetail/:id", Name: "dictionaryDetail", Component: "view/superAdmin/dictionary/sysDictionaryDetail.vue", Sort: 1, Title: "字典详情", Icon: "order"},
			{Id: 20, MenuLevel: 0, Hidden: false, ParentId: 3, Path: "operation", Name: "operation", Component: "view/superAdmin/operation/sysOperationRecord.vue", Sort: 6, Title: "操作历史", Icon: "pie-chart"},
			{Id: 21, MenuLevel: 0, Hidden: false, ParentId: 9, Path: "simpleUploader", Name: "simpleUploader", Component: "view/example/simpleUploader/simpleUploader", Sort: 6, Title: "断点续传（插件版）", Icon: "upload"},
			{Id: 22, MenuLevel: 0, Hidden: false, ParentId: 0, Path: "https://www.gin-vue-admin.com", Name: "https://www.gin-vue-admin.com", Component: "/", Sort: 0, Title: "官方网站", Icon: "home-filled"},
			{Id: 23, MenuLevel: 0, Hidden: false, ParentId: 0, Path: "state", Name: "state", Component: "view/system/state.vue", Sort: 6, Title: "服务器状态", Icon: "cloudy"},
			{Id: 24, MenuLevel: 0, Hidden: false, ParentId: 14, Path: "autoCodeAdmin", Name: "autoCodeAdmin", Component: "view/systemTools/autoCodeAdmin/index.vue", Sort: 1, Title: "自动化代码管理", Icon: "magic-stick"},
			{Id: 25, MenuLevel: 0, Hidden: true, ParentId: 14, Path: "autoCodeEdit/:id", Name: "autoCodeEdit", Component: "view/systemTools/autoCode/index.vue", Sort: 0, Title: "自动化代码（复用）", Icon: "magic-stick"},
		}
		err := db.Create(&entities).Error
		if err != nil {
			err = errors.Wrap(err, "menu  表数据初始化失败!")
			logger.Fatal(err)
		}
	}
}
func InitApiData(db *dbcontext.GormDB, logger *log.Logger) {
	var count int64
	db.Model(&permission_pb.ApiORM{}).Count(&count)
	if count == 0 {
		//  表为空的话 创建种子数据
		logger.Infof("menu表数据为空,将创建种子数据")
		entities := []permission_pb.ApiORM{
			{ApiGroup: "base", Method: "POST", Path: "/base/login", Description: "用户登录(必选)"},

			{ApiGroup: "jwt", Method: "POST", Path: "/jwt/jsonInBlacklist", Description: "jwt加入黑名单(退出，必选)"},

			{ApiGroup: "系统用户", Method: "DELETE", Path: "/user/deleteUser", Description: "删除用户"},
			{ApiGroup: "系统用户", Method: "POST", Path: "/user/register", Description: "用户注册"},
			{ApiGroup: "系统用户", Method: "POST", Path: "/user/getUserList", Description: "获取用户列表"},
			{ApiGroup: "系统用户", Method: "PUT", Path: "/user/setUserInfo", Description: "设置用户信息"},
			{ApiGroup: "系统用户", Method: "PUT", Path: "/user/setSelfInfo", Description: "设置自身信息(必选)"},
			{ApiGroup: "系统用户", Method: "GET", Path: "/user/getUserInfo", Description: "获取自身信息(必选)"},
			{ApiGroup: "系统用户", Method: "POST", Path: "/user/setUserAuthorities", Description: "设置权限组"},
			{ApiGroup: "系统用户", Method: "POST", Path: "/user/changePassword", Description: "修改密码（建议选择)"},
			{ApiGroup: "系统用户", Method: "POST", Path: "/user/setUserAuthority", Description: "修改用户角色(必选)"},
			{ApiGroup: "系统用户", Method: "POST", Path: "/user/resetPassword", Description: "重置用户密码"},

			{ApiGroup: "api", Method: "POST", Path: "/api/createApi", Description: "创建api"},
			{ApiGroup: "api", Method: "POST", Path: "/api/deleteApi", Description: "删除Api"},
			{ApiGroup: "api", Method: "POST", Path: "/api/updateApi", Description: "更新Api"},
			{ApiGroup: "api", Method: "POST", Path: "/api/getApiList", Description: "获取api列表"},
			{ApiGroup: "api", Method: "POST", Path: "/api/getAllApis", Description: "获取所有api"},
			{ApiGroup: "api", Method: "POST", Path: "/api/getApiById", Description: "获取api详细信息"},
			{ApiGroup: "api", Method: "DELETE", Path: "/api/deleteApisByIds", Description: "批量删除api"},

			{ApiGroup: "角色", Method: "POST", Path: "/authority/copyAuthority", Description: "拷贝角色"},
			{ApiGroup: "角色", Method: "POST", Path: "/authority/createAuthority", Description: "创建角色"},
			{ApiGroup: "角色", Method: "POST", Path: "/authority/deleteAuthority", Description: "删除角色"},
			{ApiGroup: "角色", Method: "PUT", Path: "/authority/updateAuthority", Description: "更新角色信息"},
			{ApiGroup: "角色", Method: "POST", Path: "/authority/getAuthorityList", Description: "获取角色列表"},
			{ApiGroup: "角色", Method: "POST", Path: "/authority/setDataAuthority", Description: "设置角色资源权限"},

			{ApiGroup: "casbin", Method: "POST", Path: "/casbin/updateCasbin", Description: "更改角色api权限"},
			{ApiGroup: "casbin", Method: "POST", Path: "/casbin/getPolicyPathByAuthorityId", Description: "获取权限列表"},

			{ApiGroup: "菜单", Method: "POST", Path: "/menu/addBaseMenu", Description: "新增菜单"},
			{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenu", Description: "获取菜单树(必选)"},
			{ApiGroup: "菜单", Method: "POST", Path: "/menu/deleteBaseMenu", Description: "删除菜单"},
			{ApiGroup: "菜单", Method: "POST", Path: "/menu/updateBaseMenu", Description: "更新菜单"},
			{ApiGroup: "菜单", Method: "POST", Path: "/menu/getBaseMenuById", Description: "根据id获取菜单"},
			{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenuList", Description: "分页获取基础menu列表"},
			{ApiGroup: "菜单", Method: "POST", Path: "/menu/getBaseMenuTree", Description: "获取用户动态路由"},
			{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenuAuthority", Description: "获取指定角色menu"},
			{ApiGroup: "菜单", Method: "POST", Path: "/menu/addMenuAuthority", Description: "增加menu和角色关联关系"},

			{ApiGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/findFile", Description: "寻找目标文件（秒传）"},
			{ApiGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/breakpointContinue", Description: "断点续传"},
			{ApiGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/breakpointContinueFinish", Description: "断点续传完成"},
			{ApiGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/removeChunk", Description: "上传完成移除文件"},

			{ApiGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/upload", Description: "文件上传示例"},
			{ApiGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/deleteFile", Description: "删除文件"},
			{ApiGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/getFileList", Description: "获取上传文件列表"},

			{ApiGroup: "系统服务", Method: "POST", Path: "/system/getServerInfo", Description: "获取服务器信息"},
			{ApiGroup: "系统服务", Method: "POST", Path: "/system/getSystemConfig", Description: "获取配置文件内容"},
			{ApiGroup: "系统服务", Method: "POST", Path: "/system/setSystemConfig", Description: "设置配置文件内容"},

			{ApiGroup: "客户", Method: "PUT", Path: "/customer/customer", Description: "更新客户"},
			{ApiGroup: "客户", Method: "POST", Path: "/customer/customer", Description: "创建客户"},
			{ApiGroup: "客户", Method: "DELETE", Path: "/customer/customer", Description: "删除客户"},
			{ApiGroup: "客户", Method: "GET", Path: "/customer/customer", Description: "获取单一客户"},
			{ApiGroup: "客户", Method: "GET", Path: "/customer/customerList", Description: "获取客户列表"},

			{ApiGroup: "代码生成器", Method: "GET", Path: "/autoCode/getDB", Description: "获取所有数据库"},
			{ApiGroup: "代码生成器", Method: "GET", Path: "/autoCode/getTables", Description: "获取数据库表"},
			{ApiGroup: "代码生成器", Method: "POST", Path: "/autoCode/createTemp", Description: "自动化代码"},
			{ApiGroup: "代码生成器", Method: "POST", Path: "/autoCode/preview", Description: "预览自动化代码"},
			{ApiGroup: "代码生成器", Method: "GET", Path: "/autoCode/getColumn", Description: "获取所选table的所有字段"},

			{ApiGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/getMeta", Description: "获取meta信息"},
			{ApiGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/rollback", Description: "回滚自动生成代码"},
			{ApiGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/getSysHistory", Description: "查询回滚记录"},
			{ApiGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/delSysHistory", Description: "删除回滚记录"},

			{ApiGroup: "系统字典详情", Method: "PUT", Path: "/sysDictionaryDetail/updateSysDictionaryDetail", Description: "更新字典内容"},
			{ApiGroup: "系统字典详情", Method: "POST", Path: "/sysDictionaryDetail/createSysDictionaryDetail", Description: "新增字典内容"},
			{ApiGroup: "系统字典详情", Method: "DELETE", Path: "/sysDictionaryDetail/deleteSysDictionaryDetail", Description: "删除字典内容"},
			{ApiGroup: "系统字典详情", Method: "GET", Path: "/sysDictionaryDetail/findSysDictionaryDetail", Description: "根据ID获取字典内容"},
			{ApiGroup: "系统字典详情", Method: "GET", Path: "/sysDictionaryDetail/getSysDictionaryDetailList", Description: "获取字典内容列表"},

			{ApiGroup: "系统字典", Method: "POST", Path: "/sysDictionary/createSysDictionary", Description: "新增字典"},
			{ApiGroup: "系统字典", Method: "DELETE", Path: "/sysDictionary/deleteSysDictionary", Description: "删除字典"},
			{ApiGroup: "系统字典", Method: "PUT", Path: "/sysDictionary/updateSysDictionary", Description: "更新字典"},
			{ApiGroup: "系统字典", Method: "GET", Path: "/sysDictionary/findSysDictionary", Description: "根据ID获取字典"},
			{ApiGroup: "系统字典", Method: "GET", Path: "/sysDictionary/getSysDictionaryList", Description: "获取字典列表"},

			{ApiGroup: "操作记录", Method: "POST", Path: "/sysOperationRecord/createSysOperationRecord", Description: "新增操作记录"},
			{ApiGroup: "操作记录", Method: "GET", Path: "/sysOperationRecord/findSysOperationRecord", Description: "根据ID获取操作记录"},
			{ApiGroup: "操作记录", Method: "GET", Path: "/sysOperationRecord/getSysOperationRecordList", Description: "获取操作记录列表"},
			{ApiGroup: "操作记录", Method: "DELETE", Path: "/sysOperationRecord/deleteSysOperationRecord", Description: "删除操作记录"},
			{ApiGroup: "操作记录", Method: "DELETE", Path: "/sysOperationRecord/deleteSysOperationRecordByIds", Description: "批量删除操作历史"},

			{ApiGroup: "断点续传(插件版)", Method: "POST", Path: "/simpleUploader/upload", Description: "插件版分片上传"},
			{ApiGroup: "断点续传(插件版)", Method: "GET", Path: "/simpleUploader/checkFileMd5", Description: "文件完整度验证"},
			{ApiGroup: "断点续传(插件版)", Method: "GET", Path: "/simpleUploader/mergeFileMd5", Description: "上传完成合并文件"},

			{ApiGroup: "email", Method: "POST", Path: "/email/emailTest", Description: "发送测试邮件"},
			{ApiGroup: "email", Method: "POST", Path: "/email/emailSend", Description: "发送邮件示例"},

			{ApiGroup: "excel", Method: "POST", Path: "/excel/importExcel", Description: "导入excel"},
			{ApiGroup: "excel", Method: "GET", Path: "/excel/loadExcel", Description: "下载excel"},
			{ApiGroup: "excel", Method: "POST", Path: "/excel/exportExcel", Description: "导出excel"},
			{ApiGroup: "excel", Method: "GET", Path: "/excel/downloadTemplate", Description: "下载excel模板"},

			{ApiGroup: "按钮权限", Method: "POST", Path: "/authorityBtn/setAuthorityBtn", Description: "设置按钮权限"},
			{ApiGroup: "按钮权限", Method: "POST", Path: "/authorityBtn/getAuthorityBtn", Description: "获取已有按钮权限"},
			{ApiGroup: "按钮权限", Method: "POST", Path: "/authorityBtn/canRemoveAuthorityBtn", Description: "删除按钮"},
		}
		err := db.Create(&entities).Error
		if err != nil {
			err = errors.Wrap(err, "menu  表数据初始化失败!")
			logger.Fatal(err)
		}
	}
}

func InitAuthorityData(db *dbcontext.GormDB, logger *log.Logger) {
	var count int64
	db.Model(&permission_pb.AuthorityORM{}).Count(&count)
	if count == 0 {
		//  表为空的话 创建种子数据
		logger.Infof("authority 表数据为空,将创建种子数据")
		entities := []permission_pb.AuthorityORM{
			{Id: 888, AuthorityName: "普通用户", ParentId: "0", DefaultRouter: "dashboard",
				MenuIds: pq.Int64Array{
					1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25,
				}},
			{Id: 9528, AuthorityName: "测试角色", ParentId: "0", DefaultRouter: "dashboard",
				MenuIds: pq.Int64Array{
					1, 2, 8,
				}},
			{Id: 8881, AuthorityName: "普通用户子角色", ParentId: "888", DefaultRouter: "dashboard",
				MenuIds: pq.Int64Array{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17}},
		}
		err := db.Create(&entities).Error
		if err != nil {
			err = errors.Wrap(err, "authority   表数据初始化失败!")
			logger.Fatal(err)
		}
	}
}
